package com.company.cloud.club.modules.db.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.cloud.club.modules.db.domain.RecommendCard;
import com.company.cloud.club.modules.db.domain.UserInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Set;

public interface RecommendCardMapper extends BaseMapper<RecommendCard> {

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND parent_id = '0' AND type = #{type, jdbcType=VARCHAR} ORDER BY time_created DESC")
    List<RecommendCard> selectTopAll(@Param("type") String type);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND parent_id = #{parentId, jdbcType=VARCHAR} ORDER BY time_created DESC")
    List<RecommendCard> selectByParentId(@Param("parentId") String parentId);

    @Delete("DELETE FROM club_recommend_card WHERE id = #{id, jdbcType=VARCHAR}")
    boolean removeById(@Param("id") String id);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND type = #{type, jdbcType=VARCHAR} " +
            "AND is_suggest = #{isSuggest, jdbcType=INTEGER} ORDER BY time_created DESC")
    List<RecommendCard> selectByTypeAndSuggest(@Param("type") String type,
                                               @Param("isSuggest") Integer isSuggest);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND file_type = #{fileType, jdbcType=VARCHAR} " +
            "AND is_suggest = #{isSuggest, jdbcType=INTEGER} ORDER BY time_created DESC")
    List<RecommendCard> selectByFileTypeAndSuggest(@Param("fileType") String fileType,
                                                   @Param("isSuggest") Integer isSuggest);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = #{isPublish} AND type IN ('主题', '手记') ORDER BY time_created DESC limit #{pageStart}, #{pageEnd} " )
    List<RecommendCard> selectAllDynamics(@Param("isPublish") Integer isPublish,
                                         @Param("pageStart") Integer pageStart,
                                         @Param("pageEnd") Integer pageEnd);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = #{isPublish} AND type IN ('主题', '手记') and is_suggest=1 ORDER BY time_created DESC limit #{pageStart}, #{pageEnd} " )
    List<RecommendCard> selectAllDynamicsToday(@Param("isPublish") Integer isPublish,
                                          @Param("pageStart") Integer pageStart,
                                          @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM club_recommend_card WHERE is_publish = #{isPublish} AND type IN ('主题', '手记') ORDER BY time_created DESC")
    int countAllDynamics(@Param("isPublish") Integer isPublish);

    @Select("SELECT * FROM club_recommend_card WHERE type IN ('主题', '手记')  ORDER BY time_created DESC limit #{pageStart}, #{pageEnd} " )
    List<RecommendCard> selectAllDynamic(@Param("pageStart") Integer pageStart,
                                         @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM club_recommend_card WHERE type IN ('主题', '手记') ORDER BY time_created DESC")
    int countAllDynamic();

    @Select({
            "<script>",
            "SELECT * FROM club_recommend_card ",
            "WHERE is_publish = 2 AND club_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "OR parent_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "OR user_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "AND type IN ('主题', '手记') ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}",
            "</script>"
    })
    List<RecommendCard> selectMyFollow(@Param("collList") Set<String> collList,
                                       @Param("pageStart") Integer pageStart,
                                       @Param("pageEnd") Integer pageEnd);

    @Select({
            "<script>",
            "SELECT count(*) FROM club_recommend_card ",
            "WHERE is_publish = 2 AND club_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "OR parent_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "OR user_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "AND type IN ('主题', '手记')",
            "</script>"
    })
    int countMyFollow(@Param("collList") Set<String> collList);

    @Select("SELECT * FROM cust_user_info WHERE id IN " +
            "(SELECT DISTINCT user_id FROM club_recommend_card " +
             "WHERE is_publish = 2 AND parent_id = #{parentId, jdbcType=VARCHAR} ) limit #{pageStart}, #{pageEnd}")
    List<UserInfo> pageUserByParentId(@Param("parentId") String parentId,
                                      @Param("pageStart") Integer pageStart,
                                      @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM cust_user_info WHERE id IN " +
            "(SELECT DISTINCT user_id FROM club_recommend_card WHERE is_publish = 2 AND parent_id = #{parentId, jdbcType=VARCHAR} )")
    Integer pageCountByParentId(@Param("parentId") String parentId);

    @Update("UPDATE club_recommend_card SET is_publish = 2 WHERE id = #{id, jdbcType=VARCHAR} ")
    boolean publishRecom(@Param("id") String id);

    @Select("SELECT * FROM club_recommend_card WHERE parent_id = #{parentId, jdbcType=VARCHAR} ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}")
    List<RecommendCard> pageByParentId(@Param("parentId") String parentId,
                                       @Param("pageStart") Integer pageStart,
                                       @Param("pageEnd") Integer pageEnd);

    @Select({
            "<script>",
            "SELECT * FROM club_recommend_card ",
            "WHERE is_publish = 2 AND user_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "AND type IN ('主题', '手记', '评论') ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}",
            "</script>"
    })
    List<RecommendCard> selectMyFriend(@Param("collList") Set<String> collList,
                                       @Param("pageStart") Integer pageStart,
                                       @Param("pageEnd") Integer pageEnd);

    @Select({
            "<script>",
            "SELECT count(*) FROM club_recommend_card ",
            "WHERE is_publish = 2 AND user_id IN ",
            "<foreach collection='collList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "AND type IN ('主题', '手记')",
            "</script>"
    })
    int countMyFriend(@Param("collList") Set<String> collList);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND club_id = #{clubId, jdbcType=VARCHAR} " +
            "ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}")
    List<RecommendCard> selectByClubId(@Param("clubId") String clubId,
                                       @Param("pageStart") Integer pageStart,
                                       @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM club_recommend_card WHERE is_publish = 2 AND club_id = #{clubId, jdbcType=VARCHAR}")
    int countByClubId(@Param("clubId") String clubId);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND user_id = #{userId, jdbcType=INTEGER} " +
            "ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}")
    List<RecommendCard> pageByUserId(@Param("userId") Integer userId,
                                     @Param("pageStart") Integer pageStart,
                                     @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM club_recommend_card WHERE is_publish = 2 AND user_id = #{userId, jdbcType=INTEGER}")
    int countByUserId(@Param("userId") Integer userId);

    @Select("SELECT * FROM club_recommend_card WHERE is_publish = 2 AND club_id = #{clubId, jdbcType=INTEGER} " +
            "AND is_essence = 1 ORDER BY time_created DESC limit #{pageStart}, #{pageEnd}")
    List<RecommendCard> pageEssenceAndClub(@Param("clubId") String clubId,
                                           @Param("pageStart") Integer pageStart,
                                           @Param("pageEnd") Integer pageEnd);

    @Select("SELECT count(*) FROM club_recommend_card WHERE is_publish = 2 AND club_id = #{clubId, jdbcType=INTEGER} " +
            "AND is_essence = 1")
    int countEssenceAndClub(@Param("clubId") String clubId);

    @Select("SELECT * FROM club_recommend_card WHERE club_id = #{clubId, jdbcType=VARCHAR} AND type = '公告' ORDER BY time_created DESC")
    List<RecommendCard> noticeList(@Param("clubId") String clubId);

}